<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form
          ref="userForm"
          label-width="220px"
          :model="form"
          :rules="rules"
        >
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="form.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input
                  v-model="form.workNumber"
                  disabled
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="form.mobile" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <cascader v-model="form.departmentId" :user-id="userId" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select
                  v-model="form.formOfEmployment"
                  size="mini"
                  class="inputW"
                >
                  <el-option :value="1" label="正式" />
                  <el-option :value="2" label="非正式" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="form.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="form.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <avatar-upload v-model="form.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left: 220px">
              <el-button
                size="mini"
                type="primary"
                @click="updateData"
              >保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import cascader from './components/cascader.vue'
import avatarUpload from './components/avatarUpload.vue'
import { changeEmployeeAPI, getUserInformation } from '@/api/staff'
import { addEmployeeAPI } from '@/api/staff.js'
export default {
  components: { cascader, avatarUpload },
  data() {
    return {
      userId: this.$route.params.id,
      form: {},
      imageUrl: 'https://pic3.zhimg.com/80/v2-5dec080026fa86e2a4b54bcd8cddeeaa_720w.webp',
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            // 验证
            validator: (rule, value, callback) => {
              console.log(value)
              if (this.form.timeOfEntry) {
                if (new Date(this.form.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                  return
                }
              }
              callback()
            }
          }
        ]
      }
    }
  },
  created() {
    this.userId && this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      // 获取用户信息
      const result = await getUserInformation(this.userId)
      this.form = result.data
      this.$set(this.form, 'roleIds', [...result.data.roleIds])
    },
    // 更新用户信息
    async updateData() {
      await this.$refs.userForm.validate()
      if (this.userId) {
        await changeEmployeeAPI(this.userId, this.form)
      } else {
        await addEmployeeAPI(this.form)
      }
      this.$refs.userForm.resetFields()
      this.$router.back()
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px;
  }
}
</style>
